/*
Date:20210530 PM
key:是菱形边上所有数值相加
*/
class Solution {
public:
    const int dic[4][2]={{1,1},{1,-1},{-1,-1},{-1,1}};
    int ijmax(vector<vector<int>>& grid,int i,int j)
    {
        
        int x=i,y=j,d=0;
        int re=0;
        while(1)
        {
            cout<<grid[x][y]<<endl;
            re+=grid[x][y];
            int newx1=x+dic[d][0];
            int newy1=y+dic[d][1];
            
            bool a=((newx1<grid.size())&&(newx1>-1));
            bool b=((newy1<grid[0].size())&&(newy1>-1));
            
            if(a&&b)
            {
                
                //re+=grid[newx1][newy1];
                x=newx1;
                y=newy1;
            }else
            {
                if(x==i&&y==j){break;}
                d=(d+1)%4;
                x=x+dic[d][0];
                y=y+dic[d][1];
                //re+=grid[x][y];
            }
            if((x==i)&&(y==j)){break;}
            
            //re+=grid[x][y];
        }
        return re;
    }
    vector<int> getBiggestThree(vector<vector<int>>& grid) 
    {
        vector<vector<int>>g=grid;
        //g.resize(grid.size());
        const int n=g.size();
        // for(int i=0;i<n;i++)
        // {
        //     g[i].resize(grid[0].size());
        // }
        const int m=g[0].size();
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                g[i][j]=ijmax(grid,i,j);
                //cout<<i<<" "<<j<<" "<<g[i][j]<<endl;
            }
        }
        int count=3;
        vector<int>v;
        while(count--)
        {
            int re=INT_MIN;
            int x,y;

            for(int i=0;i<n;i++)
            {
                for(int j=0;j<m;j++)
                {
                    
                    if(re<g[i][j])
                    {
                        re=g[i][j];
                        x=i;
                        y=j;
                    }


                }
            }
            g[x][y]=INT_MIN;
            if(v.size()==0)
            {
                v.push_back(re);
            }
            else
            {
                if(v.size()>0&&(re!=v[v.size()-1]))
                {
                    v.push_back(re);
                }
            }

            
         }
        return v;
               
    }
 
};